package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lekang.dto.BedDTO;
import com.lekang.service.ICaregiverBedService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.Bed;
import com.lekang.service.IBedService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 床位Controller
 * @author wjy
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/bed/bed")
public class BedController extends BaseController {
    @Autowired
    private IBedService bedService;

    // 新增：注入护理员-床位绑定服务
    @Autowired
    private ICaregiverBedService caregiverBedService;

    /** 查询床位列表（基础信息） */
    @PreAuthorize("@ss.hasPermi('bed:bed:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bed bed) {
        startPage();
        List<Bed> list = bedService.selectBedList(bed);
        return getDataTable(list);
    }

    /** 查询床位全关联列表（含楼层、房间、老人、护理员） */
    @PreAuthorize("@ss.hasPermi('bed:bed:list')")
    @GetMapping("/listDto")
    public TableDataInfo listDto(BedDTO bedDTO) {
        // 不分页，返回所有数据（用于负责老人页面的楼层-房间-床位展示）
        List<BedDTO> list = bedService.getBedWithAllRelations(bedDTO);
        return getDataTable(list);
    }
    /** 查询床位全关联列表（含楼层、房间、老人、护理员） */
    @PreAuthorize("@ss.hasPermi('bed:bed:list')")
    @GetMapping("/listDto1")
    public TableDataInfo listDto1(BedDTO bedDTO) {
        // 分页，返回所有数据（用于入住配置选择房间展示）
        startPage();
        List<BedDTO> list = bedService.getBedWithAllRelations(bedDTO);
        return getDataTable(list);
    }

    /** 导出床位列表（基础信息） */
    @PreAuthorize("@ss.hasPermi('bed:bed:export')")
    @Log(title = "床位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bed bed) {
        List<Bed> list = bedService.selectBedList(bed);
        ExcelUtil<Bed> util = new ExcelUtil<>(Bed.class);
        util.exportExcel(response, list, "床位数据");
    }

    /** 获取床位详细信息（基础信息） */
    @PreAuthorize("@ss.hasPermi('bed:bed:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(bedService.selectBedById(id));
    }

    /** 新增床位 */
    @PreAuthorize("@ss.hasPermi('bed:bed:add')")
    @Log(title = "床位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bed bed) {
        return toAjax(bedService.insertBed(bed));
    }

    /** 修改床位 */
    @PreAuthorize("@ss.hasPermi('bed:bed:edit')")
    @Log(title = "床位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bed bed) {
        return toAjax(bedService.updateBed(bed));
    }

    /** 删除床位 */
    @PreAuthorize("@ss.hasPermi('bed:bed:remove')")
    @Log(title = "床位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bedService.deleteBedByIds(ids));
    }

    // 新增：单个床位绑定护理员
    @PreAuthorize("@ss.hasPermi('bed:bed:edit')")
    @Log(title = "床位护理员绑定", businessType = BusinessType.UPDATE)
    @PostMapping("/{bedId}/caregivers")
    public AjaxResult bindCaregiversToBed(
            @PathVariable Long bedId,
            @RequestBody List<Long> caregiverIds) {
        caregiverBedService.bindCaregiversToBed(bedId, caregiverIds);
        return success("绑定成功");
    }

    // 新增：房间批量绑定护理员（房间内所有床位绑定相同护理员）
    @PreAuthorize("@ss.hasPermi('bed:bed:edit')")
    @Log(title = "房间护理员批量绑定", businessType = BusinessType.UPDATE)
    @PostMapping("/room/{roomId}/caregivers")
    public AjaxResult bindCaregiversToRoom(
            @PathVariable Long roomId,
            @RequestBody List<Long> caregiverIds) {
        // 1. 查询房间内所有床位ID
        List<Long> bedIds = bedService.selectBedIdsByRoomId(roomId);
        // 2. 逐个床位绑定护理员
        for (Long bedId : bedIds) {
            caregiverBedService.bindCaregiversToBed(bedId, caregiverIds);
        }
        return success("批量绑定成功");
    }

    // 新增：获取所有已被绑定的护理员ID列表
    @GetMapping("/bound-caregivers")
    public AjaxResult getBoundCaregiverIds() {
        List<Long> boundIds = caregiverBedService.getAllBoundCaregiverIds();
        return success(boundIds);
    }
}